# **********************************************************************
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.net
# *
# * Copyright (C) 2018-2022 Sandro Santilli <strk@kbt.io>
# *
# * This is free software; you can redistribute and/or modify it under
# * the terms of the GNU General Public Licence. See the COPYING file.
# *
# **********************************************************************

top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
builddir = @builddir@
srcdir = @srcdir@

PERL=@PERL@
MINGWBUILD=@MINGWBUILD@
HAVE_SFCGAL=@HAVE_SFCGAL@

POSTGIS_MAJOR_VERSION=@POSTGIS_MAJOR_VERSION@
POSTGIS_MINOR_VERSION=@POSTGIS_MINOR_VERSION@
POSTGIS_MICRO_VERSION=@POSTGIS_MICRO_VERSION@

# To use a custom database name, run:
#
#    make garden GARDENDB=custom_db_name
#
GARDENDB=postgis_garden

# Where we put our regression installation
# See https://trac.osgeo.org/postgis/ticket/5066
ifeq ($(MINGWBUILD),1)
       abs_builddir=$(shell bash -c "pwd -W")
else
       abs_builddir=$(shell pwd)
endif
REGRESS_INSTALLDIR = $(abs_builddir)/00-regress-install

# MingW hack: rather than use PGSQL_BINDIR directly, we change to the directory and
# then use "pwd" to return the path. This ensures that the returned path is in MSYS
# format, otherwise colons in drive letters will break PATH.
PGSQL_BINDIR=$(shell cd "@PGSQL_BINDIR@" && pwd)

#
# Put path from pg_config into front of search path
#
PATH := $(PGSQL_BINDIR):$(PATH)
export PATH

all install uninstall:

check: check-regress

check-unit:

include $(builddir)/core/tests.mk
include $(srcdir)/loader/tests.mk
include $(srcdir)/dumper/tests.mk

include $(srcdir)/runtest.mk

clean:
	rm -rf $(REGRESS_INSTALLDIR)
	rm -f postgis_garden_result.txt

distclean: clean
	rm -f Makefile
	rm -f core/Makefile
	rm -f core/tests.mk
	rm -f loader/Makefile
	rm -f dumper/Makefile

staged-install-sfcgal:
	@if test x"@SFCGAL@" != "x"; then \
		$(MAKE) -C ../sfcgal REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) install; \
	fi

staged-install-topology:
	@if test x"@TOPOLOGY@" != "x"; then \
		$(MAKE) -C ../topology REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) install; \
	fi

staged-install-raster:
	@if test x"@RASTER@" != "x"; then \
		$(MAKE) -C ../raster/rt_pg REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) install; \
	fi

staged-install-core:
	$(MAKE) -C ../postgis REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) install
	$(MAKE) -C ../ REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) comments-install
	#$(MAKE) -C ../loader REGRESS=1 DESTDIR=$(REGRESS_INSTALLDIR) install

staged-install:
	$(MAKE) staged-install-core
	$(MAKE) staged-install-raster
	$(MAKE) staged-install-topology
	$(MAKE) staged-install-sfcgal
	$(PERL) -pi -e 's,\$$libdir,$(REGRESS_INSTALLDIR)/lib,g' $(REGRESS_INSTALLDIR)/share/contrib/postgis/*.sql

garden: staged-install
	dropdb --if-exists $(GARDENDB)
	createdb $(GARDENDB)
	psql -qX -v ON_ERROR_STOP=true -d $(GARDENDB) < $(REGRESS_INSTALLDIR)/share/contrib/postgis/postgis.sql
	psql -qX -v ON_ERROR_STOP=true -d $(GARDENDB) < $(REGRESS_INSTALLDIR)/share/contrib/postgis/spatial_ref_sys.sql
	psql -qX -v ON_ERROR_STOP=true -d $(GARDENDB) < $(REGRESS_INSTALLDIR)/share/contrib/postgis/rtpostgis.sql
	psql -qX -v ON_ERROR_STOP=true -d $(GARDENDB) < $(REGRESS_INSTALLDIR)/share/contrib/postgis/topology.sql
	psql -qX -v ON_ERROR_STOP=true -d $(GARDENDB) < $(REGRESS_INSTALLDIR)/share/contrib/postgis/sfcgal.sql
	@echo '-------------------------------------------------'
	@echo 'PostGIS garden tests in progress (it will take time)'
	@echo '-------------------------------------------------'
	psql -X -ad $(GARDENDB) < \
		../doc/postgis_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql \
		> postgis_garden_result.txt 2>&1 || { \
		tail -n 20 postgis_garden_result.txt; false; }
	tail -n 20 postgis_garden_result.txt
	@echo '-------------------------------------------------'
	@echo 'PostGIS Raster garden tests in progress (it will take time)'
	@echo '-------------------------------------------------'
	psql -X -ad $(GARDENDB) < \
		../doc/raster_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql \
		> raster_garden_result.txt 2>&1 || { \
		tail -n 20 raster_garden_result.txt; false; }
	tail -n 20 raster_garden_result.txt
	@echo '-------------------------------------------------'
	@echo 'PostGIS Topology garden tests in progress (it will take time)'
	@echo '-------------------------------------------------'
	psql -X -ad $(GARDENDB) < \
		../doc/topology_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql \
		> topology_garden_result.txt 2>&1 || { \
		tail -n 20 topology_garden_result.txt; false; }
	tail -n 20 topology_garden_result.txt
	@echo '-------------------------------------------------'
	@echo 'PostGIS SFCGAL garden tests in progress (it will take time)'
	@echo '-------------------------------------------------'
	psql -X -ad $(GARDENDB) < \
		../doc/sfcgal_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql \
		> sfcgal_garden_result.txt 2>&1 || { \
		tail -n 20 sfcgal_garden_result.txt; false; }
	tail -n 20 sfcgal_garden_result.txt
